package fr.resasante.spring3.controller.view;

import javax.naming.NamingException;

import org.springframework.web.servlet.ModelAndView;

import fr.resasante.ejb.dto.User;
import fr.resasante.ejb.utils.EjbResponse;

/**
 * Class for getting system properties
 * 
 * @author GEFFROY-DE-VILLEBLAN
 * 
 */
public final class SettingsView {
	/**
	 * class instance
	 */
	private static volatile SettingsView instance = null;

	/**
	 * Method returning a Singleton class instance
	 * 
	 * @return Singleton instance
	 * @throws NamingException
	 */
	public final static SettingsView getInstance() {
		if (SettingsView.instance == null) {
			synchronized (SettingsView.class) {
				if (SettingsView.instance == null) {
					SettingsView.instance = new SettingsView();
				}
			}
		}
		return SettingsView.instance;
	}

	/**
	 * Constructor
	 */
	private SettingsView() {
		super();
	}

	/**
	 * Return the objects and view for password change
	 * 
	 * @param user
	 *            The user form
	 * @return The view
	 */
	public ModelAndView getPasswordChangeView(User user) {
		final ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("settings/password");
		modelAndView.addObject("password", user);
		return modelAndView;
	}

	/**
	 * Return the objects and view for password change
	 * 
	 * @param user
	 *            The user form
	 * @return The view
	 */
	public ModelAndView getAddressChangeView(User user) {
		final ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("settings/address");
		modelAndView.addObject("address", user);
		return modelAndView;
	}

	/**
	 * Return the objects and view for address change
	 * 
	 * @param user
	 *            The user form
	 * @return The view
	 */
	public ModelAndView getAddressChangeWithErrorView(User user, EjbResponse ejbResponse) {
		final ModelAndView modelAndView = getAddressChangeView(user);
		modelAndView.addObject("errorList", ejbResponse.getErrors());
		return modelAndView;
	}

	/**
	 * Return the objects and view for password change
	 * 
	 * @param user
	 *            The user form
	 * @return The view
	 */
	public ModelAndView getPasswordChangeWithErrorView(User user, EjbResponse ejbResponse) {
		final ModelAndView modelAndView = getPasswordChangeView(user);
		modelAndView.addObject("errorList", ejbResponse.getErrors());
		return modelAndView;
	}
}